package dao;

import bean.Student;
import bean.User;
import util.Dbutil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDaoImp implements StudentDao {

    @Override
    public Student StudentAdd(Student student) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1.获取连接
            connection = Dbutil.getConnection();
            //2.准备SQL语句
            String sql = "insert into t_student values(null,?,?,?,?,?)";
            //3.获取prepareStatement类型的引用
            preparedStatement = connection.prepareStatement(sql);
            //4.向问号所占的位置设置数据
            preparedStatement.setString(1,student.getName());
            preparedStatement.setString(2,student.getSex());
            preparedStatement.setString(3,student.getDate());
            preparedStatement.setString(4,student.getEmail());
            preparedStatement.setString(5,student.getRemarks());
            //5.实行sql语句
            int row = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6.关闭资源
            try {
                Dbutil.closeResource(connection,preparedStatement);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return student;
    }

    @Override
    public Student StudentDelete(Student student) {
        return null;
    }

    @Override
    public Student StudentQuery(Student student) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1.获取数据库连接
            connection = Dbutil.getConnection();
            //2.准备SQL语句
            String sql = "select * from t_student where name = ? and sex = ? and date = ? and email = ? and remarks = ?";
            //3.执行SQL语句后获取结果并返回
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,student.getName());
            preparedStatement.setString(2,student.getSex());
            preparedStatement.setString(3,student.getDate());
            preparedStatement.setString(4,student.getEmail());
            preparedStatement.setString(5,student.getRemarks());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                Student student1 = new Student(resultSet.getString("name"),resultSet.getString("sex"),resultSet.getString("date"),resultSet.getString("email"),resultSet.getString("remarks"));
                return student1; //表示查找成功
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.释放相关资源
                Dbutil.closeResource(connection,preparedStatement);
                if (null != resultSet) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null; //表示查找失败
    }
}

